class Solution:
    def threeSum(self, nums):
        nums.sort()
        res = []
        for i in range(len(nums)-2):
            if i>0 and nums[i]==nums[i-1]:
                continue
            l = i+1
            r = len(nums) - 1
            while l < r:
                temp = nums[i] + nums[r] + nums[l]
                if temp == 0:
                    res.append([nums[i],nums[l], nums[r]])
                    while l < r and nums[l] == nums[l+1]:
                        l += 1
                    while l < r and nums[r] == nums[r-1]:
                        r -= 1
                    l += 1
                    r -= 1
                if temp > 0:
                    r -= 1
                if temp < 0:
                    l += 1
        return res 
